﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ClearLines.Stocks.UnitTests
{
   using Domain;
   using NUnit.Framework;

   [TestFixture]
   public class TestsMovingAverage
   {
      [Test]
      public void MovingAverage_Of_One_Should_Be_Value_Itself()
      {
         var stockHistory = CreateTestStockHistory();

         var average = MovingAverage.Compute(stockHistory, new DateTime(2010, 1, 1), 1);
         Assert.That(average, Is.EqualTo(100d));
      }

      [Test]
      public void When_Less_Values_Than_Length_Are_Available_MovingAverage_Should_Be_Null()
      {
         var stockHistory = CreateTestStockHistory();

         var average = MovingAverage.Compute(stockHistory, new DateTime(2010, 1, 1), 2);
         Assert.That(average, Is.Null);
      }

      [Test]
      public void MovingAverage_Should_Be_Average_Of_Latest_Length_Observations()
      {
         var stockHistory = CreateTestStockHistory();

         var average = MovingAverage.Compute(stockHistory, new DateTime(2010, 1, 3), 2);
         Assert.That(average, Is.EqualTo(130d));
      }

      private static StockHistory CreateTestStockHistory()
      {
         var symbol = "SYMBOL";
         var startDate = new DateTime(2010, 1, 1);
         var endDate = new DateTime(2010, 1, 3);
         var history = new List<TradingDaySummary>()
                          {
                             new TradingDaySummary() {Day = new DateTime(2010, 1, 1), Close = 100m},
                             new TradingDaySummary() {Day = new DateTime(2010, 1, 2), Close = 120m},
                             new TradingDaySummary() {Day = new DateTime(2010, 1, 3), Close = 140m}
                          };
         var stockHistory = new StockHistory(symbol, startDate, endDate, history);
         return stockHistory;
      }
   }
}
